<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="formData" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <!-- 头部 -->
      <div class="title-container">
        <div class="title">用户登录</div>
        <div class="little-title">User login</div>
      </div>
      <!-- 用户名 -->
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="zhanghu" />
        </span>
        <el-input ref="username" v-model="formData.username" placeholder="请输入用户名" name="username" type="text" auto-complete="on" />
      </el-form-item>
      <!-- 密码 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="icon-password" />
        </span>
        <el-input ref="password" v-model="formData.password" :type="passwordShow ? '' : 'password'" placeholder="请输入密码" name="password" auto-complete="on" @keyup.enter.native="handleLogin" />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordShow ? 'eye-open' : 'eye'" />
        </span>
      </el-form-item>
      <!-- 登录按钮 -->
      <el-button :loading="loading" type="primary" class="login-btn" @click.native.prevent="handleLogin">
        立即登录
      </el-button>
    </el-form>
    <div class="footer">技术支持：阿萨姆(Assum)</div>
  </div>
</template>

<script>
// import { login } from '@/api/login/index'
import Cookies from 'js-cookie';

export default {
  name: 'Login',
  data () {
    return {
      formData: {
        username: 'luffy',
        password: 'luffy'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }]
      },
      passwordShow: false,
      loading: false,
      redirect: ''
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    showPwd () {
      this.passwordShow = !this.passwordShow;
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          Cookies.set('sessionid', '123456'); // mock模拟数据，手动设置sessionid,实际开发由后端自动设置到cookie
          Cookies.set('loginName', 'luffy');
          this.$message({
            message: '登录成功',
            type: 'success'
          });
          this.loading = false;
          this.$router.push({ path: this.redirect || '/index' });
          this.$store.dispatch('tagsView/addView', this.$route);
          // this.$http(login(this.formData), res => {
          //   // 登录成功，跳转到主页
          //   this.loading = false
          //   if (res.c === 0) {
          //     Cookies.set('sessionid', '123456') // mock模拟数据，手动设置sessionid,实际开发由后端自动设置到cookie
          //     this.$message({
          //       message: res.m,
          //       type: 'success'
          //     })
          //     this.loading = false
          //     this.$router.push({ path: this.redirect || '/index' })
          //   } else {
          //     this.$message({
          //       message: res.m || '用户名或密码错误',
          //       type: 'warning'
          //     })
          //   }
          // }, (res) => {
          //   // 登录失败
          //   this.loading = false
          // })
        } else {
          this.loading = false;
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
$font-color: rgba(255, 255, 255, 0.6);

.login-container {
  width: 100%;
  height: 100%;
  position: relative;
  background: url('../../assets/common/three-body.jpg') no-repeat;
  background-size: cover;
  overflow: hidden;
  .footer {
    position: fixed;
    left: 50%;
    bottom: 50px;
    transform: translateX(-50%);
    font-size: 16px;
    font-weight: 400;
    color: rgba(255, 255, 255, 0.6);
  }

  .login-form {
    background: rgba(255, 255, 255, 0);
    box-shadow: 0px 3px 30px 0px rgba(106, 111, 122, 0.5);
    border-radius: 10px;
    position: absolute;
    right: 15%;
    top: 50%;
    transform: translateY(-50%);
    width: 450px;
    padding: 80px 50px;
    overflow: hidden;
  }
  .title-container {
    position: relative;
    margin-bottom: 30px;
    .title {
      font-size: 24px;
      font-weight: 400;
      color: $font-color;
      text-align: left;
    }
    .little-title {
      margin-top: 9px;
      font-size: 14px;
      font-weight: 400;
      color: $font-color;
    }
  }
  .svg-container {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    color: $font-color;
    .svg-icon {
      width: 26px;
      height: 26px;
    }
  }
  .show-pwd {
    width: 40px;
    height: 40px;
    font-size: 16px;
    color: rgba(255, 255, 255, 0.6);
    cursor: pointer;
    user-select: none;
    display: flex;
    align-items: center;
    justify-content: center;
    .svg-icon {
      width: 20px;
      height: 20px;
    }
  }
  .login-btn {
    width: 100%;
    height: 40px;
    background: rgba(255, 255, 255, 0);
    box-shadow: 0px 3px 30px 0px rgba(106, 111, 122, 0.5);
    color: rgba(255, 255, 255, 0.6);
    border: 1px solid rgba(153, 153, 153, 0.5);
  }

  /* reset element-ui css */
  .el-form-item__content {
    display: flex;
  }
  .el-input {
    input {
      height: 40px;
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 0px;
      color: $font-color;
    }
  }

  .el-form-item.is-success .el-input__inner,
  .el-form-item.is-success .el-input__inner:focus,
  .el-form-item.is-success .el-textarea__inner,
  .el-form-item.is-success .el-textarea__inner:focus {
    border: none;
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.3);
    border-radius: 5px;
    margin-bottom: 30px;
  }
  .el-form-item__error {
    color: yellow;
  }
}
</style>

